package com.hzx.controller;


import com.hzx.pojo.ComPicture;
import com.hzx.pojo.Commodity;
import com.hzx.service.ComPictureService;
import com.hzx.service.CommodityService;
import com.hzx.service.ShopReleaseService;
import com.hzx.until.FileUpload;
import com.hzx.until.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ShopReleaseController {


    private static String COMPICTUREPATH = "C:\\Users\\49975\\Desktop\\数据库\\DBPJ\\spring02\\src\\main\\resources\\static\\img\\ComPicture\\" ;
    //private static String COMPICTUREPATH = "F:\\newideafile\\dbpj\\src\\main\\resources\\static\\img\\ComPicture\\" ;
    private static String ComPIC = "\\img\\ComPicture\\" ;

    @Autowired
    private ShopReleaseService shopReleaseService;

    @Autowired
    private ComPictureService comPictureService ;

    @Autowired
    private CommodityService commodityService;

    @GetMapping("/shoprelease.html")
    public String shopReleaseController() {
        return "shoprelease";
    }

    @RequestMapping(value = "/product",method = RequestMethod.POST)
    public String releaseShop(@RequestParam String name,
                              @RequestParam int count,
                              @RequestParam String price,
                              @RequestParam String o_price,
                              @RequestParam String take_way,
                              @RequestParam String describe,
                              @RequestParam Integer kid ,
                              @RequestPart("img") MultipartFile file,
                              HttpServletRequest request,
                              Model model) {

        Integer uid = (Integer) request.getSession().getAttribute("uid") ;


        Commodity commodity = new Commodity();


        if(!StringUntil.isNullOrEmpty(uid)){
            commodity.setUid(uid);
        }

        if(!StringUntil.isNullOrEmpty(count)){
            commodity.setCount(count);
        }
        if(!StringUntil.isNullOrEmpty(name)){
            commodity.setName(name);
        }
        if(!StringUntil.isNullOrEmpty(kid)){
            commodity.setKid(kid);
        }

        if(!StringUntil.isNullOrEmpty(price)){
            commodity.setPrice(Double.parseDouble(price));

        }
        if(!StringUntil.isNullOrEmpty(o_price)){
           commodity.setO_price(Double.parseDouble(o_price));

        }
        if(!StringUntil.isNullOrEmpty(take_way)){
            commodity.setTake_way(take_way);
        }
        if(!StringUntil.isNullOrEmpty(describe)){
            commodity.setDescribe(describe);
        }

        int ans = shopReleaseService.insertShopReleaseBySelective(commodity);

        Integer commoidtyId = shopReleaseService.selectIdBySelective(commodity) ; // 通过查询最新产品  获取它的id

        boolean ok = uploadfile(file,commoidtyId) ;

        if(ans==1&&ok){
            model.addAttribute(commodity) ;
            return "shoprelease" ;
        }else {
            return "修改失败" ;
        }
    }

    private boolean uploadfile(MultipartFile file,Integer commoidtyId){
        String comPicName = commoidtyId+".jpg" ;
        ComPicture comPicture = new ComPicture() ;
        comPicture.setPicture(comPicName);
        comPicture.setCid(commoidtyId)  ;
        FileUpload.File_upload(file,COMPICTUREPATH,comPicName) ;
        int ans = comPictureService.insertSelective(comPicture) ;
        return true ;
    }

    private boolean uploadfiles(MultipartFile[] file, Integer commoidtyId) {

        if(!StringUntil.isNullOrEmpty(file)) {
            for(int i=1;i<= file.length;i++) {
                ComPicture comPicture = new ComPicture() ;
                String Compicname = commoidtyId + "-" + "-" + i + ".jpg"; //  人 - 商品id - 商品图片id
                comPicture.setPicture(ComPIC+Compicname);
                comPicture.setCid(commoidtyId);
                FileUpload.File_upload(file[i],COMPICTUREPATH,Compicname) ;
                int ans = comPictureService.insertSelective(comPicture) ;
            }
        }
        return true;
    }

    // 删除商品
    @GetMapping("/delete/{id}")
    public String deleteRelease(@PathVariable("id") Integer id) {
        Integer deleteCommodity = commodityService.deleteByPrimaryKey(id);
        Integer deleteCompic = comPictureService.deleteById(id);
        if(deleteCommodity==1&&deleteCompic==1) {
            return "redirect:/personal-center";
        }
        else return "删除失败";
    }

    @GetMapping("/setCommodityStatus/{id}")
    public String setCommodityStatus(@PathVariable("id") Integer id) { // 传入该商品的 id

        commodityService.updateCommodityStatus(id); // 通过商品的 id 修改 商品的状态

        return "redirect:/personal-center";
    }

}
